﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace 渐变
{
    public partial class addUserForm : Form
    {
        /// <summary>
        /// 设置窗体的Region
        /// </summary>
        public void SetWindowRegion()
        {
            GraphicsPath FormPath;
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 50);
            this.Region = new Region(FormPath);

        }
        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }

            base.WndProc(ref m);
        }
        public addUserForm()
        {
            InitializeComponent();
        }

        private void addUserForm_Load(object sender, EventArgs e)
        {
            SetWindowRegion();
        }

        public void clearMsg()
        {
            txtuname.Text = "";
            txtuphone.Text = "";
            txtupassId.Text = "";
            txtuaddress.Text = "";
            txtMoney.Text = "";
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            string sql = "insert into uUser(uUsername,uPassword,uPhone,uAddress,umoney) values (@uUsername,@uPassword,@uPhone,@uAddress,@umoney)";
            SqlParameter[] ps =
           {
               new SqlParameter("@uUsername",txtuname.Text),
                new SqlParameter("@uPhone",txtuphone.Text),
                new SqlParameter("@uPassword",txtupassId.Text),
                new SqlParameter("@uAddress",txtuaddress.Text),
                new SqlParameter("@umoney",txtMoney.Text)
        };

            int fin = DBHelper.ExecuteNonQuery(sql, ps);
            if (fin > 0)
            {
                this.ShowSuccessTip("添加成功");
                this.DialogResult = DialogResult.OK;
                clearMsg();
            }
            else
            {
                this.ShowErrorTip("添加失败");
            }
        }

        private void btnUserCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
